//
//  MyZipTool.m
//  MyDemo
//
//  Created by macOS on 2016/12/6.
//  Copyright © 2016年 macOS. All rights reserved.
//

#import "MyZipTool.h"
#import "ZipArchive.h"

@implementation MyZipTool


//压缩
+ (void)createZipFileWithImgsArr:(NSArray *)imgsArr withSuccess:(void(^)(NSString *))success{
    
    if (imgsArr.count <=0) {
        return;
    }
   
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSMutableArray *soursPaths = [NSMutableArray array];
        [imgsArr enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSData *imgData = UIImageJPEGRepresentation(obj, 0.5);
            
            NSMutableString *path =[NSMutableString stringWithString:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];;
            NSString *imgPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"temPic_%@",@(idx)]];
            [imgData writeToFile:imgPath atomically:YES];
            [soursPaths addObject:imgPath];
        }];
  
    
            
            NSMutableString *des =[NSMutableString stringWithString:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];
            
            NSString *desPath = [des stringByAppendingPathComponent:@"tem_upload.zip"];
            //目的路径
            NSString *destinationPath = desPath;//注意是这个是 zip 格式的后缀
            //源文件路径
            //    NSString *sourceFilePath = @"/Users/Administrator/Desktop/wzg.txt";
            //数组里可以放多个源文件，这些文件会被同一打包成压缩包，到 destinationPath 这个路径下。
            if ([SSZipArchive createZipFileAtPath:destinationPath withFilesAtPaths:soursPaths]) {
                NSLog(@"压缩成功");
                
                dispatch_sync(dispatch_get_main_queue(), ^{
                    success(desPath);
                    //删除缓存 sourcepaths
                    NSFileManager *fm = [NSFileManager defaultManager];
                    [soursPaths enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        if ([fm isExecutableFileAtPath:obj]) {
                            [fm removeItemAtPath:obj error:nil];
                        }
                    }];

                
                });
             }
            else {
                
                dispatch_sync(dispatch_get_main_queue(), ^{
                    //删除缓存 sourcepaths
                    NSFileManager *fm = [NSFileManager defaultManager];
                    [soursPaths enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        if ([fm isExecutableFileAtPath:obj]) {
                            [fm removeItemAtPath:obj error:nil];
                        }
                         NSLog(@"压缩失败");
                    }];
                });
               
            }
       
      
    });
    
   }



@end
